// #ifdef  APP-PLUS || MP-WEIXIN
const baseUrl = "https://api.help10000.com";
// #endif

// #ifdef  H5
const baseUrl = "https://api.help10000.com";
// #endif

const timeout = 100000; // 超时时间设置

// 常用请求
const request = (method, url, data, ContentType) => {
	// 权限跳转添加请求头
	let header = {
		"Content-Type": ContentType ?
			ContentType :
			"application/json; charset=utf-8",
	};
	uni.showLoading({
		title: '加载中'
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method,
			data: data,
			timeout: timeout,
			header,
			sslVerify: false,
			success: (res) => {
				console.log('request res', res)
				if (res.statusCode === 200) {
					if (res.data.MsgCode != 0) {
						uni.showToast({
							title: res.data.ErrMsg || '服务出错',
							icon: "none",
							mask: true,
							position: "center",
						});
					}
					if(res.data.ErrMsg == '未登录账号'){
						uni.removeStorageSync('accountInfo');
						uni.reLaunch({
							url:'/pages/login/login'
						})
						return
					}
					resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.ErrMsg,
						icon: "none",
						mask: true,
						position: "center",
					});
					// console.log(res);
					// reject(res.message);
					resolve(res);
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '请求出错，请联系管理员',
					icon: "none",
					mask: true,
					position: "center",
				});

				reject(err);
			},
			complete: () => {
				uni.hideLoading()
				uni.stopPullDownRefresh();
			},
		});
	});
};
const uploadReq = (method, url, data) => {

	uni.showLoading({
		title: '上传中'
	})
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseUrl + url,
			method,
			formData: data.formData,
			timeout: timeout,
			filePath: data.fileUrl,
			name: data.name,
			sslVerify: false,
			success: (res) => {
				console.log('uploadReq res', res)
				if (res.statusCode === 200) {
					res.data = JSON.parse(res.data)
					if (res.data.MsgCode != 0) {
						uni.showToast({
							title: res.data.ErrMsg,
							icon: "none",
							mask: true,
							position: "center",
						});
					}
					if(res.data.ErrMsg == '未登录账号'){
						uni.removeStorageSync('accountInfo');
						uni.reLaunch({
							url:'/pages/login/login'
						})
						return
					}
					resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.ErrMsg,
						icon: "none",
						mask: true,
						position: "center",
					});
					// console.log(res);
					// reject(res.message);
					reject(res);
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '请求出错，请联系管理员',
					icon: "none",
					mask: true,
					position: "center",
				});

				reject(err);
			},
			complete: () => {
				uni.hideLoading()
			},
		});
	});
};
export {
	request,
	uploadReq
};
